/**
 * Filename: TodayShareActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/6/29 2:57
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.UIHelper;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.Log;

public class LineTimeStatusFristShareActivity extends BaseActivity implements View.OnClickListener {
    private ImageView btn_share_wx_timeline;
    private ImageView btn_share_wx_friend;
    private ImageView btn_share_qq_timeline;
    private ImageView btn_share_qq;
    private UMShareAPI mShareAPI;
    private RelativeLayout fristalbum_share;
    private RelativeLayout buttom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_fristalbum);
        mShareAPI = UMShareAPI.get(this);
        initView();
    }

    private void initView() {
        fristalbum_share = (RelativeLayout) findViewById(R.id.fristalbum_share);
        buttom = (RelativeLayout) findViewById(R.id.buttom);
        Animation translateAnimationf = new TranslateAnimation(0, 0, UIHelper.dip2px(LineTimeStatusFristShareActivity.this, 220), 0);
        translateAnimationf.setDuration(500);
        buttom.startAnimation(translateAnimationf);
        btn_share_wx_timeline = (ImageView) findViewById(R.id.btn_share_wx_timeline);
        btn_share_wx_friend = (ImageView) findViewById(R.id.btn_share_wx_friend);
        btn_share_qq_timeline = (ImageView) findViewById(R.id.btn_share_qq_timeline);
        btn_share_qq = (ImageView) findViewById(R.id.btn_share_qq);
        btn_share_wx_timeline.setOnClickListener(this);
        btn_share_wx_friend.setOnClickListener(this);
        btn_share_qq_timeline.setOnClickListener(this);
        btn_share_qq.setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
        findViewById(R.id.cancel_view).setOnClickListener(this);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        String title = getIntent().getExtras().getString("title");
        String desc = getIntent().getExtras().getString("desc");
        String url = getIntent().getExtras().getString("UrlShare");
        String avatarPaths = getIntent().getExtras().getString("avatarPaths");
        switch (v.getId()) {
            // 朋友圈
            case R.id.btn_share_wx_timeline:
                UMWeb web = new UMWeb(url);
                web.setTitle(title);//标题
                web.setThumb(new UMImage(LineTimeStatusFristShareActivity.this, avatarPaths));  //缩略图
                web.setDescription(desc);//描述
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                        .withMedia(web)
                        .share();
                break;
            // 微信好友
            case R.id.btn_share_wx_friend:
                UMWeb web2 = new UMWeb(url);
                web2.setTitle(title);
                web2.setThumb(new UMImage(this, avatarPaths));
                web2.setDescription(desc);
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withText(desc)
                        .withMedia(web2)
                        .share();
                break;
            // qq空间
            case R.id.btn_share_qq_timeline:
                UMWeb webqqzone = new UMWeb(url);
                webqqzone.setTitle(title);
                webqqzone.setThumb(new UMImage(this, avatarPaths));
                webqqzone.setDescription(desc);
                new ShareAction(this).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                        .withMedia(webqqzone)
                        .share();
                break;
            // qq
            case R.id.btn_share_qq:
                UMWeb webqq = new UMWeb(url);
                webqq.setTitle(title);
                webqq.setThumb(new UMImage(this, avatarPaths));
                webqq.setDescription(desc);
                new ShareAction(this).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                        .withMedia(webqq)
                        .share();
                break;
            case R.id.cancel_view:
            case R.id.cancel:
                Animation translateAnimationf = new TranslateAnimation(0, 0, 0, UIHelper.dip2px(LineTimeStatusFristShareActivity.this, 220));
                translateAnimationf.setDuration(500);
                buttom.startAnimation(translateAnimationf);
                translateAnimationf.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        buttom.setVisibility(View.GONE);
                        fristalbum_share.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
        }
    }

    @Override
    public void onBackPressed() {
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(LineTimeStatusFristShareActivity.this, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(LineTimeStatusFristShareActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(LineTimeStatusFristShareActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(LineTimeStatusFristShareActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    // 友盟统计
    public void onResume() {
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        super.onResume();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
